//
//  TGBottomMenu.m
//  GroupBuy
//
//  Created by wwj on 15/11/4.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGBottomMenu.h"
#import "TGBottomMenuItem.h"
#import "TGSubtitlesView.h"
#import "TGCover.h"

#define kDuration 0.3
#define kAlpha 0.6

@interface TGBottomMenu ()
@property (weak, nonatomic) TGCover *coverView;


@property (weak, nonatomic) UIView *contentView;
@end

@implementation TGBottomMenu
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        //添加遮盖
        [self addCover];
        //添加ContentView
        [self addContentView];
        //添加ScrollView
        [self addScrollView];
    }
    return self;
}

-(void)onClickItemBtn:(TGBottomMenuItem *)itemBtn{
    _itemBtn.selected = NO;
    itemBtn.selected = YES;
    _itemBtn = itemBtn;
    
    //是否有子控件
    if (itemBtn.titleArray.count) {
        [self showSubtitlesAnim];
        
    }else{
        [self hiddenSubtitlesAnim];
    }
}

#pragma -mark 显示子标题
-(void)showSubtitlesAnim{
    if (_subtitlesView == nil) {
        _subtitlesView = [[TGSubtitlesView alloc] init];
        [self setSubTitle];
    }
    _subtitlesView.frame = CGRectMake(0, kBottomMenuItemH, self.frame.size.width, _subtitlesView.frame.size.height);
    _subtitlesView.mainTile = [_itemBtn titleForState:UIControlStateNormal];
    _subtitlesView.titleArray = _itemBtn.titleArray;
    
    //第一次创建的时候显示动画
    if (_subtitlesView.superview == nil) {
        [_subtitlesView show];
    }
    [_contentView insertSubview:_subtitlesView belowSubview:_scrollerView];
    
    CGRect frame = _contentView.frame;
    frame.size.height = kBottomMenuItemH + _subtitlesView.frame.size.height;
    _contentView.frame = frame;
}

-(void)setSubTitle{}

-(void)onSelectItemBtn:(TGBottomMenuItem *)itemBtn{}

#pragma -mark 隐藏子标题
-(void)hiddenSubtitlesAnim{
    [self onSelectItemBtn:_itemBtn];
    [_subtitlesView hidden];
    _subtitlesView = nil;
    CGRect frame = _contentView.frame;
    frame.size.height = kBottomMenuItemH;
    _contentView.frame = frame;
}

#pragma -mark 添加遮盖
-(void)addCover{
    if (_coverView == nil) {
        _coverView = [TGCover coverWithTarget:self action:@selector(hidden)];
        _coverView.frame = self.bounds;
        [self addSubview:_coverView];
    }
}

-(void)addContentView{
    UIView *contentView = [[UIView alloc] init];
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    contentView.frame = CGRectMake(0, 0, self.frame.size.width, kBottomMenuItemH);
    [self addSubview:contentView];
    _contentView = contentView;
}

-(void)addScrollView{
    UIScrollView *scrollerView = [[UIScrollView alloc] init];
    scrollerView.autoresizingMask = _contentView.autoresizingMask;
    scrollerView.frame = CGRectMake(0, 0, self.frame.size.width, kBottomMenuItemH);
    scrollerView.showsHorizontalScrollIndicator = NO;
    scrollerView.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:scrollerView];
    _scrollerView = scrollerView;
}

#pragma -mark 显示动画
-(void)show{
    _contentView.transform = CGAffineTransformMakeTranslation(0, - _contentView.frame.size.height);
    _contentView.alpha = 0;
    _coverView.alpha = 0;
    [UIView animateWithDuration:kDuration animations:^{
        _contentView.transform = CGAffineTransformIdentity;
        _coverView.alpha = kAlpha;
        _contentView.alpha = 1;
    }];
}

#pragma -mark 隐藏动画
-(void)hidden{
    if (_hiddenBlock) {
        _hiddenBlock();
    }
    [UIView animateWithDuration:kDuration animations:^{
        _contentView.transform = CGAffineTransformMakeTranslation(0, - _contentView.frame.size.height);
        _coverView.alpha = 0;
        _contentView.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        _contentView.transform = CGAffineTransformIdentity;
        _coverView.alpha = kAlpha;
        _contentView.alpha = 1;
    }];
}
@end
